import React from "react";
import { Link, Outlet, Navigate, useRoutes } from "react-router-dom";

const Hot = () => {
  return <div>这是热映页面</div>;
};
const Cinema = () => {
  return <div>这是影院页面</div>;
};

const Home = () => {
  return (
    <>
      <div>这是home页面</div>
      <ul>
        <li>
          {/* <Link to="/home/hot">热映页</Link> */}
          <Link to="/home">热映页</Link>
        </li>
        <li>
          <Link to="/home/cinema">影院页</Link>
        </li>
      </ul>

      <hr />

      <Outlet />
    </>
  );
};
const About = () => {
  return <div>这是about页面</div>;
};

const routes = [
  {
    path: "/",
    element: <Navigate to="/home"></Navigate>,
  },
  {
    path: "/home",
    element: <Home />,
    children: [
      {
        path: "/home",
        element: <Navigate to="/home/hot"></Navigate>,
      },
      {
        path: "/home/hot",
        element: <Hot />,
      },
      {
        path: "/home/cinema",
        element: <Cinema />,
      },
    ],
  },
  {
    path: "/about/:id",
    element: <About />,
  },
  {
    path: "*",
    element: <div>not found..</div>,
  },
];

const App = () => {
  const element = useRoutes(routes);
  return (
    <>
      <h2>react嵌套路由</h2>

      <ul>
        <li>
          <Link to="/home">首页</Link>
        </li>
        <li>
          <Link to="/about/123">关于页</Link>
        </li>
      </ul>

      <hr />

      {element}

      {/* <Routes>
        {routes.map((item) => (
          <Route path={item.path} element={item.element} key={item.path}>
            {item.children &&
              item.children.map((val) => (
                <Route
                  path={val.path}
                  element={val.element}
                  key={val.path}
                ></Route>
              ))}
          </Route>
        ))}
      </Routes> */}

      {/* <Routes>
        <Route path="/" element={<Navigate to="/home"></Navigate>}></Route>
        <Route path="/home" element={<Home />}>
          <Route
            path="/home"
            element={<Navigate to="/home/hot"></Navigate>}
          ></Route>
          <Route path="hot" element={<Hot />}></Route>
          <Route path="/home/cinema" element={<Cinema />}></Route>
        </Route>

        <Route path="/about/:id" element={<About />}></Route>

        <Route path="*" element={<div>404 notfound...</div>}></Route>
      </Routes> */}
    </>
  );
};

export default App;
